import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:http/http.dart' as http;
import 'package:my_app/conponate/user_info.dart';

class HttpServer{
  // 10.0.2.2
  static String ipAddr = "http://172.23.230.62:8080";

  static Future<List> getData(String name) async{
    Uri url = Uri.parse(ipAddr + '/search?name=' + name + "&userName=" + UserInfo.userName);
    print(url.toString());
    http.Response responses = await http.get(url);
    print(responses.statusCode);
    print(responses.body);
    if(responses.statusCode == 200){
      return jsonDecode(responses.body);
    } else{
      throw "error";
    }
  }

  static Future<String> login(String userName, String password) async{
    Uri url = Uri.parse(ipAddr + '/login');
    print(url.toString());
    var responses = await http.post(url, body: {"userName" : userName, "password" : password});
    print(responses.statusCode);
    print(responses.body);
    if(responses.statusCode == 200){
      return responses.body;
    } else{
      throw "error";
    }
  }

  static void signUp(String userName, String password) async{
    Uri url = Uri.parse(ipAddr + '/register');
    print(url.toString());
    var responses = await http.post(url, body: {"userName" : userName, "password" : password});
    print(responses.statusCode);
    print(responses.body);

  }

  static Future<List> history(String userName) async{
    Uri url = Uri.parse(ipAddr + '/history?userName=' + userName);
    print(url.toString());
    var responses = await http.get(url);
    print(responses.statusCode);
    print(responses.body);
    if(responses.statusCode == 200){
      return jsonDecode(responses.body);
    } else{
      throw "error";
    }
  }

  static void star(String userName, String rubbishName, String flag) async{
    Uri url = Uri.parse(ipAddr + '/favourite');
    print(url.toString());
    var responses = await http.post(url, body: {"userName" : userName, "gname" : rubbishName, "star" : flag});
    print(responses.statusCode);
    print(responses.body);
  }

  static Future<List> getStar(String userName) async{
    Uri url = Uri.parse(ipAddr + '/collections?userName=' + userName);
    print(url.toString());
    var responses = await http.get(url);
    print(responses.statusCode);
    print(responses.body);
    if(responses.statusCode == 200){
      return jsonDecode(responses.body);
    } else{
      throw "error";
    }
  }

  static Future<List> postImg() async{
    String imgData = "http://43209x532p.zicp.vip/img/" + UserInfo.userName + "/001.jpg";
    String appcode = '137ba0e8959b484e959706db1c230470';
    //创建Dio
    Dio dio = new Dio();
    //初始化cotentType
    dio.options.contentType = "application/x-www-form-urlencoded; charset=UTF-8";
    //设置请求头参数
    dio.options.headers = {"Authorization" : "APPCODE " + appcode};
    String host = 'https://recover.market.alicloudapi.com';
    String path = '/recover';

    //aHR0cHM6Ly9pbWcxNC4zNjBidXlpbWcuY29tL24wL2pmcy90NjQyMS8zMS8xNzk1Nzc5NS8xODAzNTUvYzU0ZjEyZGEvNTkzN2Q2ZGJOYTAxNTI0MjQuanBn
    Map<String, dynamic> bodys = {"img" : imgData};
    String url = host + path;
    print("here1");
    var responses = await dio.post(url, queryParameters: bodys);
    print(jsonDecode(responses.data)["data"][1]["list"]);
    print("here2");
    return jsonDecode(responses.data)["data"][1]["list"];
  }

  static void uploadImage(String imagePath) async {
    print(imagePath);
    print("in upload");
    FormData formdata = FormData.fromMap({
      "file": await MultipartFile.fromFile(imagePath, filename:"001.jpg")
    });

    Dio dio = new Dio();
    var responses = await dio.post<String>(ipAddr + '/'+ UserInfo.userName + '/001.jpg', data: formdata);

    print(responses.statusCode);
    print(responses.data);

  }

  static Future<List> getHot() async{
    Uri url = Uri.parse(ipAddr + '/record');
    print(url.toString());
    var responses = await http.get(url);
    print(responses.statusCode);
    print(responses.body);
    if(responses.statusCode == 200){
      return jsonDecode(responses.body);
    } else{
      throw "error";
    }
  }

  static Future<String> changePassword(String userName, String oldPassword, String newPassword) async{
    Uri url = Uri.parse(ipAddr + '/modify');
    print(url.toString());
    var responses = await http.post(url, body: {"userName" : userName, "oldPassword" : oldPassword, "newPassword" : newPassword});
    print(responses.statusCode);
    print(jsonDecode(responses.body) as String);
    return jsonDecode(responses.body);
  }

}